local hx__xiezheng = fk.CreateSkill {
  name = "hx__xiezheng",
}

Fk:loadTranslationTable{
  ["#hx__xiezheng-choose"] = "挟征：令至多4名角色依次将一张手牌置于牌堆顶,然后你观星5并可以视为使用一张【兵临城下】！",
  ["#hx__xiezheng-ask"] = "挟征：%src 发动了挟征！请将一张手牌置于牌堆顶",
  ["#hx__xiezheng-use"] = "挟征：视为使用一张【兵临城下】！若未造成伤害，你失去1点体力",
  ["$hx__xiezheng1"] = "烈祖明皇帝乘舆仍出，陛下何妨效之。",
  ["$hx__xiezheng2"] = "陛下宜誓临戎，使将士得凭天威。",
}

hx__xiezheng:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__xiezheng.name) and target.phase == Player.Finish and
      table.find(player.room.alive_players, function (p)
        return not p:isKongcheng()
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not p:isKongcheng()
    end)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 4,
      prompt = "#hx__xiezheng-choose",
      skill_name = hx__xiezheng.name,
      cancelable = true,
    })
    if #tos > 0 then
      room:sortByAction(tos)
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(event:getCostData(self).tos) do
      if not p.dead and not p:isKongcheng() then
        local card = room:askToCards(p, {
          min_num = 1,
          max_num = 1,
          include_equip = false,
          skill_name = hx__xiezheng.name,
          prompt = "#hx__xiezheng-ask:"..player.id,
          cancelable = false,
        })
        room:moveCards({
          ids = card,
          from = p,
          toArea = Card.DrawPile,
          moveReason = fk.ReasonPut,
          skillName = hx__xiezheng.name,
        })
      end
    end
    if player.dead then return end
    -- local tar = room:askToChoosePlayers(player, {
    --   targets = room:getOtherPlayers(player),
    --   min_num = 1,
    --   max_num = 1,
    --   skill_name = hx__xiezheng.name,
    --   prompt = "请选择杀的目标",
    --   cancelable = false,
    -- })
    local card = Fk:cloneCard("slash")
    -- card.skillName = hx__xiezheng.name
    -- if not player:isProhibited(target, card) then
    --   local use = {
    --     from = player,
    --     tos = {tar[1]},
    --     card = card,
    --   }
    --   room:useCard(use)
    -- end
    room:askToGuanxing(player, {
      cards = room:getNCards(5),
      skill_name = hx__xiezheng.name,
    })
    local tar = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      skill_name = hx__xiezheng.name,
      prompt = "请选择【兵临城下】的目标",
      cancelable = true,
    })
    if #tar > 0 then
      if not player:isProhibited(target, card) and not player.dead and not tar[1].dead then
        local extra_data = {}
        local must_targets = table.filter(room:getOtherPlayers(player, false), function (p) return p.id == tar[1].id end)
        if #must_targets > 0 then
          extra_data.must_targets = table.map(must_targets, Util.IdMapper)
        end
        local use = room:askToUseVirtualCard(player, {
          name = "mobile__enemy_at_the_gates",
          skill_name = hx__xiezheng.name,
          prompt = "#mobile__xiezheng-use",
          cancelable = false,
          extra_data = extra_data,
        })
        if use and not player.dead and not (use.extra_data and use.extra_data.hx__xiezheng_damageDealt) then
          room:loseHp(player, 1, hx__xiezheng.name)
        end
      end
      player:drawCards(2, hx__xiezheng.name)
    end
  end,
})

hx__xiezheng:addEffect(fk.Damage, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card and data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent().parent
    while e do
      if e.event == GameEvent.UseCard then
        local use = e.data
        if use.card.name == "mobile__enemy_at_the_gates" and table.contains(use.card.skillNames, hx__xiezheng.name) then
          use.extra_data = use.extra_data or {}
          use.extra_data.hx__xiezheng_damageDealt = true
          return
        end
      end
      e = e.parent
    end
  end,
})

return hx__xiezheng
